#region Namespace References

using System;
using System.Windows.Forms;
using DynConDevCorp.OpenArch.SmartData.Interfaces;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.WinForms
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SmartEntityControl : UserControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SmartEntityControl"/> class.
        /// </summary>
        public SmartEntityControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Public Instance Properties

        /// <summary>
        /// Gets or sets the binding manager.
        /// </summary>
        /// <value>The binding manager.</value>
        public BindingManagerComponent BindingManager { get; set; }

        /// <summary>
        /// Gets or sets the bound entity.
        /// </summary>
        /// <value>The bound entity.</value>
        public ISmartEntity BoundEntity
        {
            get { return m_BoundEntity; }
            set
            {
                if (m_BoundEntity != value)
                {
                    m_BoundEntity = value;
                    if (m_PostLoad)
                    {
                        UpdateBindings();
                    }
                }
            }
        }

        /// <summary>
        /// Gets or sets the name of the bound field.
        /// </summary>
        /// <value>The name of the bound field.</value>
        public string BoundFieldName
        {
            get { return m_BoundFieldName; }
            set
            {
                if (m_BoundFieldName != value)
                {
                    m_BoundFieldName = value;
                    if (m_PostLoad)
                    {
                        UpdateBindings();
                    }
                }
            }
        }

        #endregion

        #region Protected Instance Properties

        /// <summary>
        /// Gets the entity binding manager.
        /// </summary>
        /// <value>The entity binding manager.</value>
        protected BindingManagerComponent EntityBindingManager
        {
            get { return bmcEntity; }
        }

        #endregion

        #region Protected Instance Methods

        /// <summary>
        /// Updates the bindings.
        /// </summary>
        protected virtual void UpdateBindings()
        {
            if ((bmcEntity != null) && !String.IsNullOrEmpty(m_BoundFieldName))
            {
                BindingManager.BoundEntityChanged += BindingManager_BoundEntityChanged;
                bmcEntity.BoundEntityChanged += bmcEntity_BoundEntityChanged;
                UpdateEntityBinding();
            }
            else if ((m_BoundEntity != null) && !String.IsNullOrEmpty(m_BoundFieldName))
            {
            }
            else if ((m_BoundEntity != null) && String.IsNullOrEmpty(m_BoundFieldName))
            {
                UpdateEntityBinding();
            }
            else
            {
            }
        }

        /// <summary>
        /// Updates the entity binding.
        /// </summary>
        protected virtual void UpdateEntityBinding()
        {
            if (BindingManager != null)
            {
                ISmartEntity parentEntity = BindingManager.BoundEntity;
                if (parentEntity != null)
                {
                    ISmartEntity childEntity = (ISmartEntity) parentEntity[BoundFieldName];
                    bmcEntity.BoundEntity = childEntity;
                }
            }
            else
            {
                bmcEntity.BoundEntity = m_BoundEntity;
            }
        }

        #endregion

        #region Private Static Methods

        private static void bmcEntity_BoundEntityChanged(object sender, EventArgs e)
        {
        }

        #endregion

        #region Private Instance Methods

        private void BindingManager_BoundEntityChanged(object sender, EventArgs e)
        {
            UpdateEntityBinding();
        }


        private void SmartControl_Load(object sender, EventArgs e)
        {
            UpdateBindings();
            m_PostLoad = true;
        }

        #endregion

        #region Fields and Constants

        private ISmartEntity m_BoundEntity;
        private string m_BoundFieldName;

        private bool m_PostLoad;

        #endregion
    }
}